﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IService.Utility;
using IService.Model;

namespace IService.User_center
{
    public partial class EditMyInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.BindData();
            }
        }

        public void BindData()
        {
            User curentUser = SessionState.CurrentUser;

            //显示联系方式
            lblRealUserName.Text = curentUser.RealUserName;
            lblRealUserName2.Text = curentUser.RealUserName;
            lblPhone.Text = curentUser.Phone;
            lblUserMobile.Text = curentUser.UserMobile;
            lblUserFax.Text = curentUser.UserFax;
            lblEmail.Text = curentUser.Email;
            lblUserAddress.Text = curentUser.UserAddress;
            lblCompanyName.Text = curentUser.CompanyName;

            //显示用户基本信息
            lblUserName.Text = curentUser.UserName;
            lblRegistDateTime.Text = curentUser.RegistDateTime.ToString("yyyy-MM-dd");
            lblLoginDate.Text = DateTime.Now.ToString("yyyy-MM-dd");

            //邦定用户角色
            if (curentUser.UserRole != null)
            {
                string[] chkSelectRole = curentUser.UserRole.Split(',');
                if (chkSelectRole.Length > 0)
                {
                    foreach (string select in chkSelectRole)
                    {
                        for (int i = 0; i < this.cblUserRole.Items.Count; i++)
                        {
                            if (string.Compare(this.cblUserRole.Items[i].Value, select, true) == 0)
                            {
                                this.cblUserRole.Items[i].Selected = true;
                            }
                        }
                    }
                }
            }

            //显示公司信息
            lblEmployeesTotal.Text = curentUser.EmployeesTotal.ToString();
            lblFoundedDate.Text = curentUser.FoundedDate.ToString("yyyy-MM-dd");
            lblCompanyPhone.Text = curentUser.CompanyPhone;
            lblCompanyAddress.Text = curentUser.CompanyAddress;

            //邦定行业分类
            if (curentUser.IndustryClassification != null)
            {
                string[] chkSelect = curentUser.IndustryClassification.Split(',');
                if (chkSelect.Length > 0)
                {
                    foreach (string select in chkSelect)
                    {
                        for (int i = 0; i < this.cblIndustryClassification.Items.Count; i++)
                        {
                            if (string.Compare(this.cblIndustryClassification.Items[i].Value, select, true) == 0)
                            {
                                this.cblIndustryClassification.Items[i].Selected = true;
                            }
                        }
                    }
                }
            }
            txtSupply.Text = curentUser.Supply;
            txtPurchase.Text = curentUser.Purchase;
            txtCompanyProfile.Text = curentUser.CompanyProfile;

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            User curentUser = SessionState.CurrentUser;
            //获取选择的会员角色checkbox
            string chkSelectRole = "";
            for (int i = 0; i < this.cblUserRole.Items.Count; i++)
            {
                if (this.cblUserRole.Items[i].Selected == true)
                    chkSelectRole += this.cblUserRole.Items[i].Value + ",";
            }
            if (chkSelectRole != "")
                chkSelectRole = chkSelectRole.Substring(0, chkSelectRole.Length - 1);
            curentUser.Phone = lblPhone.Text;
            curentUser.UserMobile = lblUserMobile.Text;
            curentUser.UserRole = chkSelectRole;
            curentUser.UserFax = lblUserFax.Text;
            curentUser.Email = lblEmail.Text;
            curentUser.UserAddress = lblUserAddress.Text;
            curentUser.RealUserName = lblRealUserName.Text;

            //获取选择的行业分类checkbox
            string chkSelect = "";
            for (int i = 0; i < this.cblIndustryClassification.Items.Count; i++)
            {
                if (this.cblIndustryClassification.Items[i].Selected == true)
                    chkSelect += this.cblIndustryClassification.Items[i].Value + ",";
            }
            if (chkSelect != "")
                chkSelect = chkSelect.Substring(0, chkSelect.Length - 1);
            curentUser.IndustryClassification = chkSelect;
            curentUser.CompanyName = lblCompanyName.Text;
            curentUser.EmployeesTotal = Convert.ToInt32(lblEmployeesTotal.Text);
            try
            {
                curentUser.FoundedDate = Convert.ToDateTime(lblFoundedDate.Text);
            }
            catch
            {
                curentUser.FoundedDate = DateTime.Now;
            }
            curentUser.CompanyPhone = lblCompanyPhone.Text;
            curentUser.CompanyAddress = lblCompanyAddress.Text;
            curentUser.Supply = txtSupply.Text;
            curentUser.Purchase = txtPurchase.Text;
            curentUser.CompanyProfile = txtCompanyProfile.Text;

            //开始保存对象到数据库中
            bool hassuccess = curentUser.Save();
            //保存成功
            if (hassuccess)
            {
                SessionState.CurrentUser = curentUser;
                JsUtil.AlertAndRedirect("保存成功!", "index.aspx", this.Page);
            }
            else
            {
                //保存失败
                JsUtil.Alert("保存失败！", this.Page);
            }
        }
    }
}